## ---- r load_table_functions

# Replication archive version - no scholr package dependency
# star_var() is defined in text_packages_rep.R

# format for tables
star_format <- case_when(
    knitr::is_latex_output() ~ "latex",
    knitr::is_html_output()  ~ "html",
    TRUE                     ~ "text"
)

xtable_format <- case_when(
    knitr::is_latex_output() ~ "latex",
    TRUE                     ~ "html"
)

kable_format <- case_when(
    knitr::is_latex_output() ~ "latex",
    knitr::is_html_output()  ~ "html",
    TRUE                     ~ "markdown"
)


kable0 <- function(...) {
    kable(...,
          format = kable_format,
          booktabs = TRUE) %>%
          kable_styling(latex_options = c("hold_position")
        )
}

# extract variable names from stargazer (uses star_var from text_packages_rep.R)
# then escapes underscores for LaTeX safety
.base_star_var <- star_var  # store reference to function from text_packages_rep.R

star_var <- function(..., omit = "pid4Oth") {
    labels <- .base_star_var(..., omit = omit)
    # Escape any remaining underscores for LaTeX safety
    labels <- stringr::str_replace_all(labels, "(?<!\\\\)_", "\\\\_")
    return(labels)
}

# custom versions of standard functions
star_cut_vector <- c(0.05, NA, NA)

star0 <- function(...) {
    stargazer(
        ...,
        digits = 3,
        header = FALSE,
        type = star_format,
        align = TRUE,
        font.size = 'scriptsize',
        star.cutoffs = star_cut_vector,
        notes.append = FALSE,
        notes = "*$p<0.05$"

    )
}


# simpler star0 to address issue with glm.nb theta not getting correct math mode
star1 <- function(...) {
    stargazer(
        ...,
        digits = 2,
        header = FALSE,
        type = star_format,
        align = TRUE,
        #font.size = 'scriptsize',
        omit.stat = c("theta"),
        star.cutoffs = star_cut_vector,
        notes.append = FALSE,
        notes = "*$p<0.05$"

    )
}

star_ft <- function(..., omit = "pid4Oth", notes = "*$p<0.05$") {
    stargazer(
        ...,
        digits = 3,
        header = FALSE,
        type = star_format,
        align = TRUE,
        font.size = 'scriptsize', #footnotesize
        #omit.stat = c("theta"),
        star.cutoffs = star_cut_vector,
        notes.append = FALSE,
        omit = omit,
        #no.space = TRUE,
        notes = notes

    )
}

star_sm <- function(...) {
    stargazer(
        ...,
        digits = 2,
        header = FALSE,
        type = star_format,
        align = TRUE,
        font.size = 'small',
        #omit.stat = c("theta"),
        star.cutoffs = star_cut_vector,
        notes.append = FALSE,
        notes = "*$p<0.05$"

    )
}

star_nrm <- function(...) {
    stargazer(
        ...,
        digits = 2,
        header = FALSE,
        type = star_format,
        align = TRUE,
        #font.size = 'small'
        #omit.stat = c("theta"),
        star.cutoffs = star_cut_vector,
        notes.append = FALSE,
        notes = "*$p<0.05$"

    )
}

print_xtable <- function(x, ...){

    xtable_form <- case_when(
        knitr::is_latex_output() ~ "latex",
        TRUE                     ~ "html"
    )
    xtab <- xtable(x)

    if(xtable_form == "latex") {
        print(xtab, ...)
    }

    if(xtable_form == "html") {
        temp_file <- tempfile(fileext = ".html")

        print(xtab, file = temp_file, type = xtable_form, ...)
        #xtab2 <- print(xtab, type = xtable_format)
        #cat(xtab2, file = temp_file)
        #writeLines(xtab2, temp_file)
        rstudioapi::viewer(temp_file)
    }
}
